package com.meiwenhui.sishuwujing;

import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.widget.TextView;

import com.meiwenhui.sishuwujing.entities.Article;
import com.orm.androrm.Filter;

public class DetailActivity extends Activity {

	private String TAG = "DetailActivity";

	private TextView mBook;
	private TextView mAuthor;
	private TextView mcontent;

	private Handler mHandler;
	private int chapterId;

	private ProgressDialog mLodingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		chapterId = getIntent().getIntExtra("chapterId", 0);
		Log.i("ttt", chapterId + "");
		mBook = (TextView) findViewById(R.id.book_name);
		mAuthor = (TextView) findViewById(R.id.book_author);
		mcontent = (TextView) findViewById(R.id.acticle_content);

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				Filter filter = new Filter();
				filter.is("chapter", chapterId);
				List<Article> articles = Article.objects(DetailActivity.this, Article.class).filter(filter).toList();
				for (Article article : articles) {
					mBook.setText(article.getBook());
					mAuthor.setText(article.getAuthor());
					Spanned text = Html.fromHtml(article.getContent());
					mcontent.setText(text);
				}

			}
		};

		mHandler.post(updateThread);
	}

	Runnable updateThread = new Runnable() {

		public void run() {

			Article article = Article.objects(DetailActivity.this, Article.class).get(chapterId);
			if (article == null) {
				Log.i(TAG, "连网查询");
				FinalHttp http = new FinalHttp();
				http.get("http://www.meiwenhui.com/api.php?action=getArticle&chapterId=" + chapterId, new DetailAjaxCallBack());
			} else {
				Log.i(TAG, "本地数据库");
				Message msg = mHandler.obtainMessage();
				Bundle bundle = new Bundle();
				bundle.putString("book", article.getBook());
				bundle.putString("author", article.getAuthor());
				bundle.putString("content", article.getContent());
				msg.setData(bundle);
				mHandler.sendMessage(msg);
			}
		}
	};

	class DetailAjaxCallBack extends AjaxCallBack<String> {

		@Override
		public void onStart() {
			super.onStart();
			mLodingDialog = ProgressDialog.show(DetailActivity.this, null, "正在加载，请稍候...", true, false);
		}

		@Override
		public void onLoading(long count, long current) {
			super.onLoading(count, current);
			System.out.println("onLoading");
		}

		@Override
		public void onSuccess(String t) {
			super.onSuccess(t);
			mLodingDialog.cancel();
			JSONArray jsonArray = null;
			try {
				jsonArray = new JSONArray(t);
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					Article article = new Article();
					article.set_id(jsonObject.getInt("id"));
					article.setAuthor(jsonObject.getString("author"));
					article.setBook(jsonObject.getString("book"));
					article.setChapter(jsonObject.getString("chapterId"));
					article.setContent(jsonObject.getString("content"));
					boolean flag = article.save(DetailActivity.this);
					Log.i("save : ", flag + "");
				}
				mHandler.sendMessage(new Message());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(Throwable t, String strMsg) {
			super.onFailure(t, strMsg);
		}

	}

}
